using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Text;
using System.Xml;
using System.IO;

namespace version1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public GameWindow gamewindow;
        public Game1()
        {

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
            graphics.PreferredBackBufferWidth = 900;
            graphics.PreferredBackBufferHeight = 720;
            graphics.ApplyChanges();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        Vector2 mousepos=new Vector2();
        List<NPC> Tuong;
        int nTuong = 0;
        MapCell map;
        Nha nhada ;
        GiaoDien giaodien;
        Tree[] cay;
        SpriteFont test;
        int ntree;
        List<Nha> VatCan=new List<Nha>();
        SoundEffect s1;
        protected override void LoadContent()
        {
           
            test = Content.Load<SpriteFont>("text");
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Random rr=new Random();
            // string[] temp = new string[21];
            List<string> t2 = new List<string>();

            StreamReader re = File.OpenText("DiaHinh.txt");
            string input = null;
            while ((input = re.ReadLine()) != null)
            {
                t2.Add(input);              
            }
            re.Close();

            StreamReader re1 = File.OpenText("matran.txt");
            int r = int.Parse(re1.ReadLine());
            int c = int.Parse(re1.ReadLine());
            int[,] cell = new int[r, c];

            for (int i = 0; i < r; i++)
            {
                if (i == 19)
                    ntree = 1;
                string line = re1.ReadLine();

                string[] g = line.Split(new char[] { });
                for (int j = 0; j < c; j++)
                {
                    cell[i, j] = int.Parse(g[j]);
                }
            }
            re.Close();

            
            map = new MapCell(Content, t2, 3, r, c, cell, new Vector2(0, 0));
            ntree = 1;
            cay = new Tree[ntree];
            for (int i = 0; i <ntree; i++)
            {
                cay[i]=new Tree(Content,"Tree",new Vector2(693,56));
            }



            Tuong = new List<NPC>();
            NPC temp1 = new NPC(Content, "KB", 50, new Vector2(Window.ClientBounds.Width/ 2, Window.ClientBounds.Height / 2), new Vector2(34, 82));
            Tuong.Add(temp1);
            nTuong = Tuong.Count;
            
            string strnha = "NhaDa"; 
            nhada = new Nha(Content, strnha, new Vector2(282,42));
            
            strnha = "ThanhBar";
            giaodien = new GiaoDien(Content,strnha,Vector2.Zero);

            strnha = "Rock";
            Nha temp = new Nha(Content, strnha, new Vector2(250, 42));
            VatCan.Add(temp);
            strnha = "Rock";

            temp = new Nha(Content, strnha, new Vector2(245, 28));
            VatCan.Add(temp);

            //s1 = Content.Load<SoundEffect>(@"Sound\14");
            //SoundEffectInstance soundEffectInstance = s1.CreateInstance();
            //soundEffectInstance.Play();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        MouseState prev=new MouseState();
        bool drag = false;
        protected override void Update(GameTime gameTime)
        {
            
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            KeyboardState kbstate = Keyboard.GetState();
            if (kbstate.IsKeyDown(Keys.Escape))
                this.Exit();
            MouseState mouseState = Mouse.GetState();
            mousepos.X = mouseState.X;
            mousepos.Y = mouseState.Y;

            Vector2 move = map.TopLeft;

            if (kbstate.IsKeyDown(Keys.Left))
            {
                map.Update(gameTime, -5, 0);
                nhada.Update(gameTime, -5, 0);              
                for (int i = 0; i < ntree; i++)
                {
                    cay[i].Update(gameTime, -5, 0);
                }
                Tuong[0].Update(gameTime, 3);//5 la huong len
                for (int i = 0; i < VatCan.Count; i++)
                    VatCan[i].Update(gameTime, -5, 0);
            }
            if (kbstate.IsKeyDown(Keys.Right) )
            {
                map.Update(gameTime, 5, 0);
                nhada.Update(gameTime, 5, 0);
                for (int i = 0; i < ntree; i++)
                {
                    cay[i].Update(gameTime, 5, 0);
                }
                Tuong[0].Update(gameTime);
                for (int i = 0; i < VatCan.Count; i++)
                    VatCan[i].Update(gameTime, 5, 0);
            }
            if (kbstate.IsKeyDown(Keys.Up) )
            {
                map.Update(gameTime, 0, -5);
                nhada.Update(gameTime, 0, -5);
                for (int i = 0; i < ntree; i++)
                {
                    cay[i].Update(gameTime, 0, -5);
                }
                //Tuong[0].Update(gameTime);
                Tuong[0].Update(gameTime, 5);//5 la huong len
                for (int i = 0; i < VatCan.Count; i++)
                    VatCan[i].Update(gameTime, 0, -5);
            }
            if (kbstate.IsKeyDown(Keys.Down) )
            {
                map.Update(gameTime, 0, 5);

                nhada.Update(gameTime, 0, 5);
                for (int i = 0; i < ntree; i++)
                {
                    cay[i].Update(gameTime, 0, 5);
                }
                Tuong[0].Update(gameTime, 1);//1 la huong xuong
                for (int i = 0; i < VatCan.Count; i++)
                    VatCan[i].Update(gameTime, 0, 5);
            }
           
           
            
            //if (kbstate.IsKeyDown(Keys.A))
            //{
            //    if (zoom <= 2.0f)
            //    {
            //        zoom += 0.001f;
            //        map.scale=zoom;
            //    }
            //}
            //if (kbstate.IsKeyDown(Keys.D))
            //{
            //    if (zoom >= 0.5f)
            //    {
            //        zoom -= 0.001f;
            //        map.scale = zoom;
            //    }
            //}

            //map.TopLeft = move;
           
            // TODO: Add your update logic here
           // for (int i = 0; i < noption; i++)
           //{
           //     option[i].Update(gameTime);
           // }
           // map.Update(gameTime);
           
           // base.Update(gameTime);
        }

        //private void SelectedIndex(ref MouseState mstate, ref int idx)
        //{
        //    Vector2 point = new Vector2(mstate.X, mstate.Y);
        //    for (int i = 0; i < noption; i++)
        //        if (option[i].isclick(point))
        //        {
        //            option[i].Delay = 100000.0f;
        //           idx = i;
        //            break;
        //        }
        //}

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            SpriteFont test;
            test=Content.Load<SpriteFont>("text");
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
           
            map.Draw(spriteBatch, gameTime);
            
            nhada.Draw(spriteBatch, gameTime);
            for (int i = 0; i < ntree; i++)
            {
                cay[i].Draw(spriteBatch, gameTime);
            }
            for (int i = 0; i < nTuong; i++)
                Tuong[i].Draw(spriteBatch,gameTime);
            string str = mousepos.X.ToString() + ", " + mousepos.Y.ToString();
            spriteBatch.DrawString(test, str, new Vector2(200,200), Color.Red);
            giaodien.Draw(spriteBatch,gameTime);
            for (int i = 0; i < VatCan.Count; i++)
                VatCan[i].Draw(spriteBatch, gameTime);

            spriteBatch.Draw(Content.Load<Texture2D>("Image//cursor"), mousepos, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.End();
            // TODO: Add your drawing code here
            base.Draw(gameTime);
        }
    }
}
